package com.summer.thread;

public class SynchronizedTest {
	/**
	 * synchronized 同步锁是作用域同一个对象的*/
	public static void main(String[] args) {
//		new SynchronizedStaticDemol().doTest1();
//		new SynchronizedStaticDemol().doTest1();
		new SynchronizedNormalDemol().doTest1();
		
//		SynchronizedBlockDemol.doTest1();
//		SynchronizedMixDemol.doTest1();
		
		try {
			Thread.sleep(8000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static class SynchronizedStaticDemol{
		
		public static void doTest(){
			new Thread(new Runnable(){
				@Override
				public void run() {
					SynchronizedStaticDemol.fool1();
				}
			}).start();
			
			new Thread(new Runnable(){
				@Override
				public void run() {
					SynchronizedStaticDemol.fool2();
				}
			}).start();
		}
		
		public void doTest1(){
			new Thread(new Runnable(){
				@Override
				public void run() {
					SynchronizedStaticDemol.doTest();
				}
			}).start();
		}
		
		public synchronized static void fool1(){
			System.out.println("Static fool1");
			try {
				Thread.sleep(2000);
				System.out.println("Static Wake up");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		public synchronized static void fool2(){
			System.out.println("Static fool2");
		}
	}
	
	public static class SynchronizedNormalDemol{
		
		public static void doTest(){
			new Thread(new Runnable(){
				@Override
				public void run() {
					new SynchronizedNormalDemol().fool1();
				}
			}).start();
			
			new Thread(new Runnable(){
				@Override
				public void run() {
					new SynchronizedNormalDemol().fool2();
				}
			}).start();
		}
		
		public static void doTest1(){
			final SynchronizedNormalDemol synchronizedNormalDemol= new SynchronizedNormalDemol();
			new Thread(new Runnable(){
				@Override
				public void run() {
					synchronizedNormalDemol.fool1();
				}
			}).start();
			
			new Thread(new Runnable(){
				@Override
				public void run() {
					synchronizedNormalDemol.fool2();
				}
			}).start();
		}
		
		
		public synchronized void fool1(){
			System.out.println("Normal fool1");
			try {
				Thread.sleep(2000);
				System.out.println("Normal Wake up");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		public synchronized void fool2(){
			System.out.println("Normal fool2");
		}
	}

	public static class SynchronizedBlockDemol{
		private final static Object[] lock = new Object[0];
		public static void doTest(){
			new Thread(new Runnable(){
				@Override
				public void run() {
					new SynchronizedBlockDemol().fool1();
				}
			}).start();
			
			new Thread(new Runnable(){
				@Override
				public void run() {
					new SynchronizedBlockDemol().fool2();
				}
			}).start();
		}
		
		public static void doTest1(){
			final SynchronizedBlockDemol synchronizedNormalDemol= new SynchronizedBlockDemol();
			new Thread(new Runnable(){
				@Override
				public void run() {
					synchronizedNormalDemol.fool1();
				}
			}).start();
			
			new Thread(new Runnable(){
				@Override
				public void run() {
					synchronizedNormalDemol.fool2();
				}
			}).start();
		}
		
		public void fool1(){
			synchronized(lock){
				System.out.println("Block fool1");
				try {
					Thread.sleep(2000);
					System.out.println("Block Wake up");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		public void fool2(){
			synchronized(lock){
				System.out.println("Block fool2");
			}
		}
	}
	
	public static class SynchronizedMixDemol{ 
		public static void doTest(){
			new Thread(new Runnable(){
				@Override
				public void run() {
					SynchronizedMixDemol.fool1();
				}
			}).start();
			
			new Thread(new Runnable(){
				@Override
				public void run() {
					new SynchronizedMixDemol().fool2();
				}
			}).start();
		}
		
		public static void doTest1(){
			final SynchronizedMixDemol synchronizedMixDemol=new SynchronizedMixDemol();
			new Thread(new Runnable(){
				@Override
				public void run() {
					synchronizedMixDemol.fool3();
				}
			}).start();
			
			new Thread(new Runnable(){
				@Override
				public void run() {
					synchronizedMixDemol.fool4();
				}
			}).start();
		}
		 
		public synchronized static void fool1(){ 
			System.out.println("Mix fool1");
			try {
				Thread.sleep(2000);
				System.out.println("Mix Wake up");
			} catch (InterruptedException e) {
				e.printStackTrace();
			} 
		}
		public void fool2(){
			synchronized(SynchronizedMixDemol.class){
				System.out.println("Mix fool2");
			}
		}
		
		
		public synchronized void fool3(){ 
			System.out.println("Mix fool3");
			try {
				Thread.sleep(2000);
				System.out.println("Mix Wake up");
			} catch (InterruptedException e) {
				e.printStackTrace();
			} 
		}
		public void fool4(){
			synchronized(this){
				System.out.println("Mix fool4");
			}
		}
	}
}
